﻿Public Class frm_settings

    Dim _settings_file_path As String = String.Empty

    Public Event SettingsUpdated(ByVal _new_settings As Settings)

    Private Sub cmd_choose_download_dir_Click(sender As Object, e As EventArgs) Handles cmd_choose_download_dir.Click

        If Not FBD_Download_Dir.ShowDialog = Windows.Forms.DialogResult.Cancel Then
            txt_download_dir.Text = FBD_Download_Dir.SelectedPath
        End If

    End Sub

    Private Sub cmd_save_settings_Click(sender As Object, e As EventArgs) Handles cmd_save_settings.Click

        Dim _tools As New Tools
        Dim _settings As New Settings
        Dim _unrar_pwd_list As New List(Of String)

        Try

            _settings.GetSetDownloadDirectory = txt_download_dir.Text

            If ComB_Existing_File_Handling.SelectedIndex = 0 Then
                _settings.GetSetExistingFileHandling = FTP.FTP.FileAction.ResumeOrCreate
            End If

            If ComB_Existing_File_Handling.SelectedIndex = 1 Then
                _settings.GetSetExistingFileHandling = FTP.FTP.FileAction.CreateNew
            End If

            _settings.GetSetSend2Tray = chk_send2tray.Checked

            _settings.GetSetUpdateCheck = chk_update_check.Checked

            _settings.GetSetPreventStandby = chk_prevent_standby.Checked

            _settings.GetSetCreateDownloadDir = chk_create_sub_folder.Checked
            _settings.GetSetCreateDownloadDirUseDescription = rd_sub_folder_description.Checked
            _settings.GetSetCreateDownloadDirUseFilename = rd_sub_folder_filename.Checked
            _settings.GetSetMaxDownloadThreads = num_max_threads.Value

            'UnRAR Settings
            _settings.GetSetDeleteAfterUnRAR = chk_unrar_del_files.Checked
            _settings.GetSetUnRARAfterDownload = chk_unrar_auto.Checked
            _settings.GetSetUseUnRARPasswordList = chk_unrar_pwd_list.Checked

            For Each _pwd In ListUnRARPwd.Items
                _unrar_pwd_list.Add(_pwd.ToString)
            Next

            _settings.GetSetUnRARPasswordList = _unrar_pwd_list


            _settings.GetSetShowSpeedreportAfterDownload = chk_show_speedreport.Checked
            _settings.GetSetSpeedreportUsername = txt_speed_var_username.Text
            _settings.GetSetSpeedreportConnection = txt_speed_var_connection.Text
            _settings.GetSetSpeedreportComment = txt_speed_var_comment.Text
            _settings.GetSetSpeedreportTemplate = rtb_speedreport.Text


            _settings.GetSetClicknLoad = chk_clicknload.Checked

            _tools.XMLSerialize(_settings, _settings_file_path)

            RaiseEvent SettingsUpdated(_settings)

            MessageBox.Show("Einstellungen erfolgreich gespeichert!", "Einstellungen gespeichert", MessageBoxButtons.OK, MessageBoxIcon.Information)

            Me.Close()

        Catch ex As Exception
            MessageBox.Show("Fehler beim speichern der Einstellungen!" & vbNewLine & "Fehler: " & ex.Message, "Fehler", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try

    End Sub

    Private Sub frm_settings_Load(sender As Object, e As EventArgs) Handles MyBase.Load

        Dim _settings As New Settings
        Dim _tools As New Tools

        Try

            If _tools.isLinux = False Then

                _settings_file_path = IO.Path.Combine(Environment.GetEnvironmentVariable("appdata"), "SFDL.NET")

                If IO.Directory.Exists(_settings_file_path) = False Then
                    IO.Directory.CreateDirectory(_settings_file_path)
                End If

                _settings_file_path = IO.Path.Combine(Environment.GetEnvironmentVariable("appdata"), "SFDL.NET\settings.xml")

            Else
                _settings_file_path = (IO.Path.Combine(Application.StartupPath, "settings.xml"))
            End If

            If IO.File.Exists(_settings_file_path) = False Then
                Throw New Exception("Keine Einstellungen gefunden!")
            End If

            _settings = _tools.XMLDeSerialize(_settings, _settings_file_path)

            txt_download_dir.Text = _settings.GetSetDownloadDirectory

            If _settings.GetSetExistingFileHandling = FTP.FTP.FileAction.ResumeOrCreate Then
                ComB_Existing_File_Handling.SelectedIndex = 0
            End If

            If _settings.GetSetExistingFileHandling = FTP.FTP.FileAction.CreateNew Then
                ComB_Existing_File_Handling.SelectedIndex = 1
            End If

            chk_send2tray.Checked = _settings.GetSetSend2Tray

            chk_update_check.Checked = _settings.GetSetUpdateCheck

            chk_prevent_standby.Checked = _settings.GetSetPreventStandby

            chk_create_sub_folder.Checked = _settings.GetSetCreateDownloadDir
            rd_sub_folder_description.Checked = _settings.GetSetCreateDownloadDirUseDescription
            rd_sub_folder_filename.Checked = _settings.GetSetCreateDownloadDirUseFilename

            If _settings.GetSetCreateDownloadDirUseDescription = False And _settings.GetSetCreateDownloadDirUseFilename = False Then
                rd_sub_folder_filename.Checked = True
            End If

            If _settings.GetSetMaxDownloadThreads = 0 Then
                num_max_threads.Value = 1
            Else
                num_max_threads.Value = _settings.GetSetMaxDownloadThreads
            End If


            'UnRAR Settings
            chk_unrar_pwd_list.Checked = _settings.GetSetUseUnRARPasswordList
            chk_unrar_auto.Checked = _settings.GetSetUnRARAfterDownload
            chk_unrar_del_files.Checked = _settings.GetSetDeleteAfterUnRAR

            ListUnRARPwd.Items.Clear()

            For Each _pwd In _settings.GetSetUnRARPasswordList
                ListUnRARPwd.Items.Add(_pwd)
            Next

            chk_show_speedreport.Checked = _settings.GetSetShowSpeedreportAfterDownload

            txt_speed_var_username.Text = _settings.GetSetSpeedreportUsername
            txt_speed_var_connection.Text = _settings.GetSetSpeedreportConnection
            txt_speed_var_comment.Text = _settings.GetSetSpeedreportComment
            rtb_speedreport.Text = _settings.GetSetSpeedreportTemplate

            chk_clicknload.Checked = _settings.GetSetClicknLoad

        Catch ex As Exception
            MessageBox.Show(ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Warning)
        End Try

    End Sub

    Private Sub cmd_add_password_Click(sender As Object, e As EventArgs) Handles cmd_add_password.Click

        Dim _pwd As String = String.Empty

        _pwd = InputBox("Kennwort:", "Kennwort hinzufügen")

        If String.IsNullOrEmpty(_pwd) = False Then
            ListUnRARPwd.Items.Add(_pwd)
        End If

    End Sub

    Private Sub cmd_del_password_Click(sender As Object, e As EventArgs) Handles cmd_del_password.Click

        For x = 0 To ListUnRARPwd.Items.Count - 1

            If ListUnRARPwd.GetSelected(x) = True Then
                ListUnRARPwd.Items.RemoveAt(x)
            End If

        Next

    End Sub

    Private Sub cmd_choose_download_dir_Click_1(sender As Object, e As EventArgs)

        If Not FBD_Download_Dir.ShowDialog = Windows.Forms.DialogResult.Cancel Then
            txt_download_dir.Text = FBD_Download_Dir.SelectedPath
        End If

    End Sub

    Private Sub chk_create_sub_folder_CheckedChanged(sender As Object, e As EventArgs) Handles chk_create_sub_folder.CheckedChanged
        If chk_create_sub_folder.Checked Then
            rd_sub_folder_description.Enabled = True
            rd_sub_folder_filename.Enabled = True
        Else
            rd_sub_folder_description.Enabled = False
            rd_sub_folder_filename.Enabled = False
        End If
    End Sub

    Private Sub cmd_insert_var_username_Click(sender As Object, e As EventArgs) Handles cmd_insert_var_username.Click
        InsertTextAtCursor("%%USER%%")
    End Sub

    Private Sub cmd_insert_var_connection_Click(sender As Object, e As EventArgs) Handles cmd_insert_var_connection.Click
        InsertTextAtCursor("%%CONNECTION%%")
    End Sub

    Private Sub cmd_insert_var_comment_Click(sender As Object, e As EventArgs) Handles cmd_insert_var_comment.Click
        InsertTextAtCursor("%%COMMENT%%")
    End Sub

    Private Sub DownloadGeschwindigkeitToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles DownloadGeschwindigkeitToolStripMenuItem.Click
        InsertTextAtCursor("%%SPEED%%")
    End Sub

    Private Sub BeschreibungToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles BeschreibungToolStripMenuItem.Click
        InsertTextAtCursor("%%SFDL_DESC%%")
    End Sub

    Private Sub UpperToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles UpperToolStripMenuItem.Click
        InsertTextAtCursor("%%SFDL_UPPER%%")
    End Sub


    Private Sub InsertTextAtCursor(ByVal _text As String)

        rtb_speedreport.Text = rtb_speedreport.Text.Insert(rtb_speedreport.SelectionStart, _text)

        rtb_speedreport.SelectionStart = rtb_speedreport.Text.Length

        rtb_speedreport.ScrollToCaret()

    End Sub


    Private Sub BenötigteDownloadzeitToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles BenötigteDownloadzeitToolStripMenuItem.Click
        InsertTextAtCursor("%%TIME%%")
    End Sub
End Class